package model

import (
	"go-caipu/pkg/services/admin/sysuser"
	"go-caipu/pkg/services/dto"
)

type SysUser struct {
	UserId   int64     `json:"userId" gorm:"primaryKey;comment:编码"`
	Username string    `json:"username" gorm:"type:varchar(64);comment:用户名"`
	Password string    `json:"-" gorm:"type:varchar(128);comment:密码"`
	NickName string    `json:"nickName" gorm:"type:varchar(128);comment:昵称"`
	Phone    string    `json:"phone" gorm:"type:varchar(128);comment:手机号"`
	Salt     string    `json:"salt" gorm:"type:varchar(255);comment:加盐"`
	Avatar   string    `json:"avatar" gorm:"type:varchar(255);comment:头像"`
	Sex      int       `json:"gender" gorm:"type:int;comment:性别"`
	Email    string    `json:"email" gorm:"type:varchar(128);comment:邮箱"`
	DeptId   int64     `json:"deptId" gorm:"type:bigint(20);comment:部门"`
	PostId   int       `json:"postId" gorm:"type:bigint(20);comment:岗位"`
	Remark   string    `json:"remark" gorm:"type:varchar(255);comment:备注"`
	Status   int       `json:"status" gorm:"type:int;comment:状态"`
	Dept     *SysDept  `json:"dept"`
	RoleIds  []string  `json:"roleIds" gorm:"-"`
	Roles    []SysRole `json:"roles" gorm:"many2many:sys_user_role;foreignKey:UserId;joinForeignKey:user_id;References:RoleId;joinReferences:role_id"`
	//创建人
	CreateUser SysUserBaseInfo `gorm:"foreignKey:CreateBy;references:UserId"`
	//更新人
	UpdateUser SysUserBaseInfo `gorm:"foreignKey:UpdateBy;references:UserId"`
	ThirdUser  SysUserThird    `json:"-" gorm:"-"`

	dto.ControlBy
	dto.ModelTime
}

func (u *SysUser) TableName() string {
	return "sys_user"
}

func (u *SysUser) GenerateDto(dto *sysuser.User) {

	dto.UserId = int64(u.UserId)
	dto.Username = u.Username
	dto.NickName = u.NickName
	dto.Avatar = u.Avatar
	dto.Status = u.Status
	dto.Password = u.Password
	dto.Salt = u.Salt
	//角色 后续用来支持多角色
	dto.RoleIds = u.RoleIds
}

type SysUserRole struct {
	UserId int64 `json:"userId" gorm:"comment:编码"`
	RoleId int64 `json:"roleId" gorm:"comment:编码"`
}

func (u *SysUserRole) TableName() string {
	return "sys_user_role"
}

type SysUserBaseInfo struct {
	UserId   int64  `json:"userId" gorm:"primaryKey;comment:编码"`
	Username string `json:"username" gorm:"type:varchar(64);comment:用户名"`
	NickName string `json:"nickName" gorm:"type:varchar(128);comment:昵称"`
}

type SysUserThird struct {
	UserId    int64  `json:"userId" gorm:"primaryKey;comment:编码"`
	ThirdId   string `json:"thirdId" gorm:"type:varchar(50);comment:第三方id"`
	ThirdType string `json:"thirdType" gorm:"type:varchar(30);comment:第三方类型"`
}

func (u *SysUserThird) TableName() string {
	return "sys_user_third"
}

func (u *SysUserBaseInfo) TableName() string {
	return "sys_user"
}
